<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `activitydocumentmap` (
	`activityid` int(11) NOT NULL,
	`documentid` int(11) NOT NULL,INDEX(`activityid`, `documentid`)) ENGINE=MyISAM;
*/

/**
* <b>ActivityDocumentMap</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @copyright Free for personal & commercial use. (Offered under the BSD license)
*/
class ActivityDocumentMap
{
	public $activityId = '';

	public $documentId = '';

	public $pog_attribute_type = array(
		"activityId" => array('db_attributes' => array("NUMERIC", "INT")),
		"documentId" => array('db_attributes' => array("NUMERIC", "INT")));
		public $pog_query;
	
	
	/**
	* Creates a mapping between the two objects
	* @param Activity $object 
	* @param Document $otherObject 
	* @return 
	*/
	function AddMapping($object, $otherObject)
	{
		if ($object instanceof Activity && $object->activityId != '')
		{
			$this->activityId = $object->activityId;
			$this->documentId = $otherObject->documentId;
			return $this->Save();
		}
		else if ($object instanceof Document && $object->documentId != '')
		{
			$this->documentId = $object->documentId;
			$this->activityId = $otherObject->activityId;
			return $this->Save();
		}
		else
		{
			return false;
		}
	}
	
	
	/**
	* Removes the mapping between the two objects
	* @param Object $object 
	* @param Object $object2 
	* @return 
	*/
	function RemoveMapping($object, $otherObject = null)
	{
		$connection = Database::Connect();
		if ($object instanceof Activity)
		{
			$this->pog_query = "delete from `activitydocumentmap` where `activityid` = '".$object->activityId."'";
			if ($otherObject != null && $otherObject instanceof Document)
			{
				$this->pog_query .= " and `documentid` = '".$otherObject->documentId."'";
			}
		}
		else if ($object instanceof Document)
		{
			$this->pog_query = "delete from `activitydocumentmap` where `documentid` = '".$object->documentId."'";
			if ($otherObject != null && $otherObject instanceof Activity)
			{
				$this->pog_query .= " and `activityid` = '".$otherObject->activityId."'";
			}
		}
		Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Physically saves the mapping to the database
	* @return 
	*/
	function Save()
	{
		$connection = Database::Connect();
		$this->pog_query = "select `activityid` from `activitydocumentmap` where `activityid`='".$this->activityId."' AND `documentid`='".$this->documentId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows == 0)
		{
			$this->pog_query = "insert into `activitydocumentmap` (`activityid`, `documentid`) values ('".$this->activityId."', '".$this->documentId."')";
		}
		return Database::InsertOrUpdate($this->pog_query, $connection);
	}
}
?>